﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using ApiScanner.Core;
using InfiniTec;

namespace ApiScannerShell.ViewModels
{
    public sealed class ExchangeStoreScannerTestConfigurationElementViewModel : TestConfigurationElementViewModel<ExchangeStoreScannerTestConfigurationElementViewModel, ExchangeStoreScannerTestConfigurationElement>
    {
        private readonly ServerNameConfigurationElementCollectionViewModel _Servers;

        public ICommand AddServerCommand { get; private set; }
        public ICommand RemoveServerNameCommand { get; private set; }

        public string Domain
        {
            get { return GetValue(v => v.Domain); }
            set { SetValue(v => v.Domain, value); }
        }

        public bool ScanPrivateFolders
        {
            get { return GetValue(v => v.ScanPrivateFolders); }
            set { SetValue(v => v.ScanPrivateFolders, value); }
        }

        public bool ScanPublicFolders
        {
            get { return GetValue(v => v.ScanPublicFolders); }
            set { SetValue(v => v.ScanPublicFolders, value); }
        }

        public bool ScanAllServers
        {
            get { return GetValue(v => v.ScanAllServers); }
            set { SetValue(v => v.ScanAllServers, value); }
        }

        public ServerNameConfigurationElementCollectionViewModel Servers
        {
            get { return GetChildViewModel(v => v.Servers); }
        }

        public ExchangeStoreScannerTestConfigurationElementViewModel(ExchangeStoreScannerTestConfigurationElement element, Action<IViewModel<TestConfigurationElement>> removeAction) : base(element, removeAction)
        {
            _Servers = new ServerNameConfigurationElementCollectionViewModel(element.Servers);
            AddSimpleMapping(v => v.Domain, () => Domain.IsNullOrEmpty() ? "The root domain of your organization must be specified." : null);
            AddSimpleMapping(v => v.ScanPrivateFolders, () => null);
            AddSimpleMapping(v => v.ScanPublicFolders, () => null);

            AddValidationRule(v => v.ScanAllServers, () => !ScanAllServers && !Servers.Any()
                                  ? "At least one server must be specified if not all servers are to be scanned."
                                  : null);

            RegisterChildViewModel(v => v.Servers, _Servers);

            LoadValuesFromModel();

            AddServerCommand = new DelegateCommand<string>(s => !string.IsNullOrEmpty(s), _Servers.Add);
            RemoveServerNameCommand = new DelegateCommand<IList>(items => items != null && items.Count > 0, items => _Servers.Remove(items.Cast<ServerNameConfigurationElementViewModel>()));
        }

        public override void SaveValuesToModel()
        {
            if(ScanAllServers)
            {
                _Servers.Clear();
            }
            base.SaveValuesToModel();
        }

        public override void LoadValuesFromModel()
        {
            base.LoadValuesFromModel();
            ScanAllServers = !_Servers.Any();
        }

        public override string DisplayName
        {
            get { return "Exchange store scanner"; }
        }

        public override string Description
        {
            get { return "Scans Exchange servers in the organization for usages of Store Event Sinks or WebForms."; }
        }
    }
}